﻿using ClientWebSocketDemo.WebApi.Models;

namespace ClientWebSocketDemo.WebApi.Services
{
    class HeartbeatService
    {
        HeartbeatChannelPicker _picker;

        private int Interval
        {
            get
            {
                return 30;
            }
        }

        public HeartbeatService(IConfigClient client)
        {
            _picker = new HeartbeatChannelPicker(client);
        }

        public void Start(Func<string> getId)
        {
            Task.Factory.StartNew(async () =>
            {
                while (true)
                {
                    var uniqueId = getId();
                    if (!string.IsNullOrEmpty(uniqueId))
                    {
                        var channel = _picker.Pick();
                        await channel.SendAsync(uniqueId);
                    }

                    await Task.Delay(1000 * Interval);
                }
            }, TaskCreationOptions.LongRunning);
        }
    }
}
